package staff.console;

import java.io.InputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;

import staff.logic.EmployeSystem;

public class EmployeConsole {
    private Map<String, EmployeSystemCommand> commands = new LinkedHashMap<String, EmployeSystemCommand>();

    private static PrintStream outStream = System.out;
    private static InputStream inStream = System.in;

    public EmployeConsole(EmployeSystem employeSystem) {
        commands.put("quit", new QuitSystem(employeSystem));
        commands.put("load_txt", new LoadFromTxt(employeSystem));
        commands.put("save_txt", new SaveToTxt(employeSystem));
        commands.put("load_dom_xml", new DOMLoadFromXML(employeSystem));
        commands.put("save_dom_xml", new DOMSaveToXML(employeSystem));
        commands.put("load_sax_xml", new SAXLoadFromXML(employeSystem));
        commands.put("save_mysql", new MySQLSaveToDB(employeSystem));
        commands.put("clear", new Clear(employeSystem));
        commands.put("remove", new Remove(employeSystem));
        commands.put("print", new PrintAll(employeSystem, outStream));
        commands.put("print_employe",
                new PrintEmploye(employeSystem, outStream));
        commands.put("print_manager",
                new PrintManager(employeSystem, outStream));
        commands.put("print_subordinate", new PrintSubordinate(employeSystem,
                outStream));
        commands.put("set_manager", new SetManager(employeSystem));
        commands.put("set_as_manager", new SetAsManager(employeSystem));
        commands.put("set_as_worker", new SetAsWorker(employeSystem));
        commands.put("set_as_other", new SetAsOther(employeSystem));
        commands.put("sort_by_id", new Sort(employeSystem));
        commands.put("sort_by_name", new SortByName(employeSystem));
        commands.put("sort_by_hire", new SortByHireDate(employeSystem));
        commands.put("help", new PrintHelp(commands, outStream));
    }

    public void start() {
        Scanner scanner = new Scanner(inStream);
        while (true) {
            outStream.print("# ");
            String commandString = scanner.nextLine();
            if (!commandString.trim().isEmpty()) {
                try {
                    runCommand(commandString);
                } catch (Exception ex) {
                    outStream.println("Error! " + ex);
                }
            }
        }
    }

    private void runCommand(String commandText) {
        String[] commandParts = commandText.trim().split("\\s+");
        if (commandParts.length > 0) {
            String commandName = commandParts[0];
            EmployeSystemCommand command = commands.get(commandName);
            if (command != null) {
                String[] params = null;
                if (commandParts.length > 1) { // exists arguments for command
                    params = Arrays.copyOfRange(commandParts, 1,
                            commandParts.length);
                }
                command.execute(params);
            } else {
                throw new UnsupportedOperationException("Unknown command: "
                        + commandName);
            }
        }
    }

    public static void main(String[] args) {
        new EmployeConsole(new EmployeSystem()).start();
    }

}
